package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.OrderVo;
import com.mmall.pojo.User;
import com.mmall.service.IOrderService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by hanchao on 2018/2/22.
 */
@RestController
@RequestMapping("/manage/order")
public class OrderManageController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IOrderService iOrderService;

    @RequestMapping("list.do")
    public ServerResponse<PageInfo> orderList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            ServerResponse.createdByErrorMessage("用户未登陆，请登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //是管理员
            return iOrderService.manageList(pageNum,pageSize);
        }else{
            return ServerResponse.createdByErrorMessage("无权限操作，需要管理员权限");
        }
    }
    @RequestMapping("detail.do")
    public ServerResponse<OrderVo> detail(HttpSession session, Long orderNo ){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            ServerResponse.createdByErrorMessage("用户未登陆，请登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //是管理员
            return iOrderService.manageDetail(orderNo);
        }else{
            return ServerResponse.createdByErrorMessage("无权限操作，需要管理员权限");
        }
    }
    @RequestMapping("search.do")
    public ServerResponse<PageInfo> manageSearch(HttpSession session,@RequestParam(value = "pageNum") Long orderNo, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            ServerResponse.createdByErrorMessage("用户未登陆，请登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //是管理员
            return iOrderService.manageSearch(orderNo,pageNum,pageSize);
        }else{
            return ServerResponse.createdByErrorMessage("无权限操作，需要管理员权限");
        }
    }
    @RequestMapping("send_goods.do")
    public ServerResponse<String> sendGoods(HttpSession session, Long orderNo ){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            ServerResponse.createdByErrorMessage("用户未登陆，请登陆");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //是管理员
            return iOrderService.manageSendGoods(orderNo);
        }else{
            return ServerResponse.createdByErrorMessage("无权限操作，需要管理员权限");
        }
    }
}
